<script setup>
	import {
		onLaunch,
		onShow,
		onError
	} from '@dcloudio/uni-app';
	import {
		computed,
		ref
	} from 'vue';
	import {
		ShoproInit
	} from './sheep';
	import sheep from '@/sheep';
    const version=ref()
	onLaunch(() => {
		// 隐藏原生导航栏 使用自定义底部导航
		uni.hideTabBar();
		
		//版本更新
		// #ifdef APP
		   // 检查版本更新
			// 获取manifest.json里的配置信息
		   plus.runtime.getProperty(plus.runtime.appid, function(appInfo) { 
			// console.log(appInfo);
			   version.value=appInfo.version
			   console.log(version.value);
			    getVersion()
		   }) 
		// #endif

		// 加载Shopro底层依赖
		ShoproInit();
	});
	
	
	const getVersion=()=>{
		sheep.$api.login.updateApk().then(response=>{
			console.log(response);
			if(version.value !=response.rows[0].tag){
				uni.showModal({
					content:'当前监测到有新的版本，确定升级至新的版本？',
					success:function (res) {
						if (res.confirm) {
								console.log('用户点击确定');
								//判断手机的机型
								let platform = uni.getSystemInfoSync().platform;
								console.log(platform);
								if(platform=='android'){
									uni.showLoading({ title: "更新中……",  });
									//下载
									downloadFile(response.rows[0].visitUrl)
									
								}else if(platform=='ios'){
									uni.showToast({
										icon:"none",
										title:'请去appStore商店下载最新版本'
									})
									return false
								}
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
					},
					fail:function(err){
							console.log(error);
						}
				})
			}
		})
	}
	const downloadFile=(data)=>{
		console.log(data);
			uni.downloadFile({
				url:data,
				 success: (downloadFile) => {
					  uni.hideLoading();
					  console.log(downloadFile);
					  if(downloadFile.statusCode==200){
						  plus.runtime.install(
						  downloadFile.tempFilePath,{
							  force:true
						  },
						  function(e){
							  plus.runtime.restart()
						  }
						  )
							
					  }
				 },
				 fail(err) {
					uni.hideLoading()
					uni.showToast({
						title:err,
						icon:'none'
					})
					console.log(err);
				 }
			})
	}

	onError((err) => {
		console.log('AppOnError:', err);
	});
	onShow(() => {
		// #ifdef APP-PLUS
		// 获取urlSchemes参数
		const args = plus.runtime.arguments;
		if (args) {}

		// 获取剪贴板
		uni.getClipboardData({
			success: (res) => {},
		});
		// #endif
	});
</script>

<style lang="scss">
	/* #ifndef APP-NVUE */
	   @import '@/sheep/scss/index.scss';
	/* #endif */
</style> 
